package com.infore.dataAudit.service.audit.impl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.infore.dataAudit.entity.TDataAuditRecord;
import com.infore.dataAudit.service.audit.IAuditExportService;
import com.infore.dataAudit.service.audit.IDataAuditService;
import com.infore.dataAudit.util.ResultEntity;
import com.infore.dataAudit.util.excel.ExcelUtil;
import com.infore.dataAudit.util.excel.entity.ExcelEntity;
import com.infore.dataAudit.util.excel.entity.ExcelTd;
import com.infore.dataAudit.util.excel.entity.ExcelTr;
import com.infore.dataAudit.vo.AuditRecordQuery;

@Service
public class AuditExportServiceImpl implements IAuditExportService {
    @Autowired
    private IDataAuditService dataAuditService;
    
    @Override
    public byte[] autoStationMultiReportExport() {
        ExcelEntity en = new ExcelEntity();
        List<ExcelTr> head = new ArrayList<>();
        ExcelTr row = new ExcelTr();
        List<ExcelTd> ths = new ArrayList<>();      
        ExcelTr row2 = new ExcelTr();
        ExcelTr row3 = new ExcelTr();
        List<ExcelTd> ths2 = new ArrayList<>();  
        List<ExcelTd> ths3 = new ArrayList<>(); 
        row3.setTds(ths3);
        row.setTds(ths);
        row2.setTds(ths2);
        head.add(row);
        head.add(row2);
        head.add(row3);
        en.setHead(head);  
        ExcelTd th = new ExcelTd();
        th.setText("序号");
        th.setWidth(10);
        th.setRowspan(3);
        ths.add(th);
        ths2.add(null);
        ths3.add(null);
        
        
        th = new ExcelTd();
        th.setText("日期");
        th.setWidth(20);
        th.setRowspan(3);
        ths.add(th);
        ths2.add(null); 
        ths3.add(null);
        
        th = new ExcelTd();
        th.setText("零点校正液核查");
        th.setWidth(40);
        th.setColspan(4);
        ths.add(th);
        
        th = new ExcelTd();
        th.setText("测定结果");
        th.setWidth(30);
        th.setColspan(2);
        ths2.add(th);
       // ths2.add(null);
        
        th = new ExcelTd();
        th.setText("绝对误差");
        th.setWidth(30);
        th.setRowspan(2);
        ths2.add(th);
       
        
        th = new ExcelTd();
        th.setText("绝对误差范围");
        th.setWidth(40);
        th.setRowspan(2);
        ths2.add(th);
       
        th = new ExcelTd();
        th.setText("系统");
        th.setWidth(20);
        ths3.add(th);
        
        th = new ExcelTd();
        th.setText("零点校正液浓度");
        th.setWidth(40);
        ths3.add(th);
        ths3.add(null); 
        ths3.add(null); 
        
        th = new ExcelTd();
        th.setText("量程校正液核查");
        th.setWidth(40);
        th.setColspan(4);
        ths.add(th);
        
        th = new ExcelTd();
        th.setText("测定结果");
        th.setWidth(30);
        th.setColspan(2);
        ths2.add(th);
        
        th = new ExcelTd();
        th.setText("相对误差");
        th.setWidth(30);
        th.setRowspan(2);
        ths2.add(th);
       
        
        th = new ExcelTd();
        th.setText("相对误差范围");
        th.setWidth(40);
        th.setRowspan(2);
        ths2.add(th);
       
        th = new ExcelTd();
        th.setText("系统");
        th.setWidth(20);
        ths3.add(th);
        
        th = new ExcelTd();
        th.setText("量程校正液浓度");
        th.setWidth(40);
        ths3.add(th);
        ths3.add(null); 
        ths3.add(null); 
             
        List<ExcelTr> body = new ArrayList<>();
        row = new ExcelTr();
        List<ExcelTd> tds = new ArrayList<>();
        int index =0;
        String title="质控数据报表";
          
        en.setBody(body);       
        en.setTitle(title);
        en.setColSize(ths3.size());
        //en.setSubTitle("子标题-1-1");
        return ExcelUtil.exportExcelEntity(en);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public byte[] auditRecordDatasExport(AuditRecordQuery query) {
        query.setPage(false);
        ResultEntity re = dataAuditService.queryDataAuditRecordPage(query);
        Map<String, Object> mapO = (Map<String, Object>) re.getContent();
        List<TDataAuditRecord> list = (List<TDataAuditRecord>) mapO.get("dataList");
        ExcelEntity en = new ExcelEntity();
        List<ExcelTr> head = ExcelUtil.getAuditRecordHead();
        en.setHead(head);       
        List<ExcelTr> body = new ArrayList<>();
        ExcelTr row = null;
        List<ExcelTd> tds = new ArrayList<>();
        ExcelTd td = null;
        int index =0;
        String title="审核日志报表";
        List<ExcelTd> ths = head.get(0).getTds();
        String value = null, filedName = null;
        Field field = null;
        for (TDataAuditRecord record : list) {
            index ++;
            row = new ExcelTr();
            body.add(row);
            tds = new ArrayList<>();
            row.setTds(tds);
            //序号
            td = new ExcelTd();
            td.setText(index+"");
            tds.add(td);
            for (ExcelTd th : ths) {
                filedName = th.getFileName();
                if(filedName != null) {
                    try {
                        field = record.getClass().getDeclaredField(filedName);
                    } catch (NoSuchFieldException e) {
                        System.out.println(filedName + "属性不存在!===");
                       continue;
                    } catch (SecurityException e) {
                        continue;
                    }
                    // 对象的属性的访问权限设置为可访问
                    field.setAccessible(true);
                    // 获取属性的对应的值
                    try {
                        value = field.get(record).toString();
                        td = new ExcelTd();
                        td.setText(value);
                        tds.add(td);
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                
            }
            
        }
        en.setBody(body);
        en.setTitle(title);
        en.setColSize(tds.size());
        return ExcelUtil.exportExcelEntity(en);
    }

}
